﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.ZhiJian
{
    public partial class 产品理化参数 : System.Web.UI.Page
    {
        private string sql;

        //处理有传入入库单ID参数的子函数
        protected void ruku_proess() {

            
            string rukuid = Request.QueryString["rukuid"];
            DropDownList1.DataSourceID = "SqlDataSource2";
            DropDownList1.DataBind();
            //如果有传送入库单参数则
            if (rukuid != null)
            {
                LuHao0.Text = DropDownList1.SelectedValue;
                LuHao0.Visible = false;

                //组合一个诸如WHERE IN ('AAAA','BBBB')的字符串
                        ListItemCollection luhaos = new ListItemCollection();
                        luhaos = DropDownList1.Items;
                        string tempsql="(";
                        foreach(var row in luhaos){
                            tempsql =tempsql+"'"+row+"',";
                        }
                        tempsql = tempsql + ")";
                        tempsql = tempsql.Replace(",)",")");
                //=================================

                    sql = @"SELECT [ID],[炉号], [C], [Mn], 
                               [Si], [P], [S], [Cr], [Ni], [Cu], 
                               [Mo], [Al], [V], [Bi], [Sn], [Pb], [As], [Sb], 
                               [抗拉强度], [屈服极限], [伸长率] 
                        FROM [产品理化参数] 
                        WHERE [炉号] IN "+tempsql;


                this.Panel2.GroupingText = "修 改";
                this.Panel2.Visible = true;
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                SqlDataAdapter sda = new SqlDataAdapter("Select * From 产品理化参数 where 炉号='" + DropDownList1.SelectedValue + "'", Conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                //如果取得炉号所对应的值则
                if (ds.Tables[0].Rows.Count != 0)
                {

                    LuHao0.Text = ds.Tables[0].Rows[0][2].ToString();
                    C.Text = ds.Tables[0].Rows[0][3].ToString();
                    Mn.Text = ds.Tables[0].Rows[0][4].ToString();
                    Si.Text = ds.Tables[0].Rows[0][5].ToString();
                    P.Text = ds.Tables[0].Rows[0][6].ToString();
                    S.Text = ds.Tables[0].Rows[0][7].ToString();
                    Cr.Text = ds.Tables[0].Rows[0][8].ToString();
                    Ni.Text = ds.Tables[0].Rows[0][9].ToString();
                    Cu.Text = ds.Tables[0].Rows[0][10].ToString();
                    Mo.Text = ds.Tables[0].Rows[0][11].ToString();
                    Al.Text = ds.Tables[0].Rows[0][12].ToString();
                    V.Text = ds.Tables[0].Rows[0][13].ToString();
                    Bi.Text = ds.Tables[0].Rows[0][14].ToString();
                    Sn.Text = ds.Tables[0].Rows[0][15].ToString();
                    Pb.Text = ds.Tables[0].Rows[0][16].ToString();
                    As.Text = ds.Tables[0].Rows[0][17].ToString();
                    Sb.Text = ds.Tables[0].Rows[0][18].ToString();
                    klqd.Text = ds.Tables[0].Rows[0][19].ToString();
                    qsjx.Text = ds.Tables[0].Rows[0][20].ToString();
                    scl.Text = ds.Tables[0].Rows[0][21].ToString();
                    Panel0.Visible = false;
                    this.Edit.Visible = false;
                    this.Find.Visible = false;
                    this.Save.Visible = true;
                    this.Add.Visible = false;
                    this.Delete.Visible = false;
                    this.Back.Visible = true;
                }
                //如果没有取到任何炉号信息则
                else
                {

                    C.Text = "";
                    Mn.Text = "";
                    Si.Text = "";
                    P.Text = "";
                    S.Text = "";
                    Cr.Text = "";
                    Ni.Text = "";
                    Cu.Text = "";
                    Mo.Text = "";
                    Al.Text = "";
                    V.Text = "";
                    Bi.Text = "";
                    Sn.Text = "";
                    Pb.Text = "";
                    As.Text = "";
                    Sb.Text = "";
                    klqd.Text = "";
                    qsjx.Text = "";
                    scl.Text = "";
                    //this.Label1.Text = this.LuHao.Text;
                    //this.Label2.Text = this.LuHao.Text;
                    this.Panel2.Visible = true;
                    this.Panel2.GroupingText = "添 加";
                    Panel0.Visible = false;
                    this.Edit.Visible = false;
                    this.Find.Visible = false;
                    this.Save.Visible = true;
                    this.Add.Visible = false;
                    this.Delete.Visible = false;
                    this.Back.Visible = true;
                }
            }
            //如果没有给传递入库单ID参数则
            else
            {

                DropDownList1.Visible = false;

            }       
        
        }

        //炉号下拉列表处理
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
            string rukuid = Request.QueryString["rukuid"];
            if (rukuid != null)
            {
                LuHao0.Text = DropDownList1.SelectedValue;
                LuHao0.Visible = false;


                this.Panel2.GroupingText = "修 改";
                this.Panel2.Visible = true;
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                SqlDataAdapter sda = new SqlDataAdapter("Select * From 产品理化参数 where 炉号='" + DropDownList1.SelectedValue + "'", Conn);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                //如果取得炉号所对应的值则
                if (ds.Tables[0].Rows.Count != 0)
                {

                    LuHao0.Text = ds.Tables[0].Rows[0][2].ToString();
                    C.Text = ds.Tables[0].Rows[0][3].ToString();
                    Mn.Text = ds.Tables[0].Rows[0][4].ToString();
                    Si.Text = ds.Tables[0].Rows[0][5].ToString();
                    P.Text = ds.Tables[0].Rows[0][6].ToString();
                    S.Text = ds.Tables[0].Rows[0][7].ToString();
                    Cr.Text = ds.Tables[0].Rows[0][8].ToString();
                    Ni.Text = ds.Tables[0].Rows[0][9].ToString();
                    Cu.Text = ds.Tables[0].Rows[0][10].ToString();
                    Mo.Text = ds.Tables[0].Rows[0][11].ToString();
                    Al.Text = ds.Tables[0].Rows[0][12].ToString();
                    V.Text = ds.Tables[0].Rows[0][13].ToString();
                    Bi.Text = ds.Tables[0].Rows[0][14].ToString();
                    Sn.Text = ds.Tables[0].Rows[0][15].ToString();
                    Pb.Text = ds.Tables[0].Rows[0][16].ToString();
                    As.Text = ds.Tables[0].Rows[0][17].ToString();
                    Sb.Text = ds.Tables[0].Rows[0][18].ToString();
                    klqd.Text = ds.Tables[0].Rows[0][19].ToString();
                    qsjx.Text = ds.Tables[0].Rows[0][20].ToString();
                    scl.Text = ds.Tables[0].Rows[0][21].ToString();
                    Panel0.Visible = false;
                    this.Edit.Visible = false;
                    this.Find.Visible = false;
                    this.Save.Visible = true;
                    this.Add.Visible = false;
                    this.Delete.Visible = false;
                    this.Back.Visible = true;
                }
                //如果没有取到任何炉号信息则
                else
                {

                    C.Text = "";
                    Mn.Text = "";
                    Si.Text = "";
                    P.Text = "";
                    S.Text = "";
                    Cr.Text = "";
                    Ni.Text = "";
                    Cu.Text = "";
                    Mo.Text = "";
                    Al.Text = "";
                    V.Text = "";
                    Bi.Text = "";
                    Sn.Text = "";
                    Pb.Text = "";
                    As.Text = "";
                    Sb.Text = "";
                    klqd.Text = "";
                    qsjx.Text = "";
                    scl.Text = "";
                    //this.Label1.Text = this.LuHao.Text;
                    //this.Label2.Text = this.LuHao.Text;
                    this.Panel2.Visible = true;
                    this.Panel2.GroupingText = "添 加";
                    Panel0.Visible = false;
                    this.Edit.Visible = false;
                    this.Find.Visible = false;
                    this.Save.Visible = true;
                    this.Add.Visible = false;
                    this.Delete.Visible = false;
                    this.Back.Visible = true;
                }
            }
            //如果没有给传递入库单ID参数则
            else
            {

                DropDownList1.Visible = false;

            }       
      
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            sql = @"SELECT [ID],[炉号], [C], [Mn], [Si], [P], [S], [Cr], [Ni], [Cu], [Mo], [Al], [V], [Bi], [Sn], [Pb], [As], [Sb], [抗拉强度], [屈服极限], [伸长率] FROM [产品理化参数] where 产品ID="+Request.QueryString["type"];
            this.Message.Text = "·准备就绪";
            if (!this.IsPostBack)
            {
                ruku_proess();
                this.SqlDataSource1.SelectCommand = sql;
                this.ViewState["SelectCommand"] = sql;
                this.ChanPin.SelectedValue=Request.QueryString["type"] ;
                                
            }
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
 
         
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);

        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        //新增一个炉号
        protected void Add_Click(object sender, EventArgs e)
        {
            LuHao0.Text = "";
            C.Text = "";
            Mn.Text = "";
            Si.Text = "";
            P.Text = "";
            S.Text = "";
            Cr.Text = "";
            Ni.Text = "";
            Cu.Text = "";
            Mo.Text = "";
            Al.Text = "";
            V.Text = "";
            Bi.Text = "";
            Sn.Text = "";
            Pb.Text = "";
            As.Text = "";
            Sb.Text = "";
            klqd.Text = "";
            qsjx.Text = "";
            scl.Text = "";     
            //this.Label1.Text = this.LuHao.Text;
            //this.Label2.Text = this.LuHao.Text;
            this.Panel2.Visible = true;
            this.Panel2.GroupingText = "添 加";
            Panel0.Visible = false;
            this.Edit.Visible = false;
            this.Find.Visible = false;
            this.Save.Visible = true;
            this.Add.Visible = false;
            this.Delete.Visible = false;
            this.Back.Visible = true;
        }
        //protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        // // ((TextBox)GridView1.Rows[GridView1.SelectedIndex].Cells[1].FindControl("TextBox1")).ReadOnly = false;
        //}

        //保存
        protected void Save_Click(object sender, EventArgs e)
        {
            if (LuHao0.Text == "")
            {
                this.Message.Text = ".请输入炉号！";
                return;
            }     
            try
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                Conn.Open();
              
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                
                Cmd.CommandText = "理化参数";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.Add(new SqlParameter("@产品ID", SqlDbType.Int, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.ChanPin.SelectedValue));
                Cmd.Parameters.Add(new SqlParameter("@炉号", SqlDbType.NVarChar, 20, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, this.LuHao0.Text));
                Cmd.ExecuteNonQuery();
                SqlCommand Cmd1 = new SqlCommand();
                Cmd1.Connection = Conn;
                Cmd1.CommandText = @"INSERT INTO [SCM].[dbo].[产品理化参数]
           ([产品ID]
           ,[炉号]
           ,[C]
           ,[Mn]
           ,[Si]
           ,[P]
           ,[S]
           ,[Cr]
           ,[Ni]
           ,[Cu]
           ,[Mo]
           ,[Al]
           ,[V]
           ,[Bi]
           ,[Sn]
           ,[Pb]
           ,[As]
           ,[Sb]
           ,[抗拉强度]
           ,[屈服极限]
           ,[伸长率]
           ,[横向冲击]
           ,[纵向冲击]
           ,[通径检验]
           ,[静水压试验]
           ,[无损探伤]
           ,[表面检验]
           ,[尺寸])
     VALUES(" + this.ChanPin.SelectedValue + ",'" + this.LuHao0.Text + "','" +
                  C.Text + "','" + Mn.Text + "','" + Si.Text + "','" + P.Text + "','" + S.Text + "','" +
                  Cr.Text + "','" + Ni.Text + "','" + Cu.Text + "','" + Mo.Text + "','" + Al.Text + "','" +
                  V.Text + "','" + Bi.Text + "','" + Sn.Text + "','" + Pb.Text + "','" + As.Text + "','" +
                  Sb.Text + "','" + klqd.Text + "','" + qsjx.Text + "','" + scl.Text + "','合格','合格','合格','合格','合格','合格','合格')";
                Cmd1.ExecuteNonQuery();

                this.Message.Text = ".操作成功！";
                this.LuHao0.Text = "";
                 C.Text = "";
                 Mn.Text="";
                 Si.Text="";
                 P.Text="";
                 S.Text = "";
                 Cr.Text ="";
                 Ni.Text="";
                 Cu.Text="";
                 Mo.Text="";
                 Al.Text = "";
                 V.Text ="";
                 Bi.Text="";
                 Sn.Text="";
                 Pb.Text ="";
                 As.Text ="";
                 Sb.Text="";
                 klqd.Text="";
                 qsjx.Text="";
                 scl.Text = "";
            }
            catch (Exception ex)
            {
                this.Message.Text = ex.Message;
            }
            String sql = @"SELECT [ID],[炉号], [C], [Mn], [Si], [P], [S], [Cr], [Ni], [Cu], [Mo], [Al], [V], [Bi], [Sn], [Pb], [As], [Sb], [抗拉强度], [屈服极限], [伸长率] FROM [产品理化参数] where 产品ID="+Request.QueryString["type"];
            this.SqlDataSource1.SelectCommand = sql;
            this.GridView1.DataBind();
            this.Edit.Visible = true;
            this.Find.Visible = true;
            this.Add.Visible = true;
            this.Save.Visible = false;
            this.Back.Visible = false;
            this.Delete.Visible = true;
            this.Panel0.Visible = true;
            this.Panel2.Visible = false;
            this.LuHao0.Text = "";
        }

        //删除一个炉号
        protected void Delete_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
            this.Add.Visible = true;
            this.Save.Visible = false;
           // this.Back.Visible = false;
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            {
                this.SqlDataSource1.DeleteCommand = "Exec 理化参数_Delete " + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                this.SqlDataSource1.DeleteCommandType = SqlDataSourceCommandType.Text;
                this.SqlDataSource1.Delete();
                this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand"];
                this.Message.Text = "·删除成功";
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }

        //查询一个炉号的信息
        protected void Find_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
            this.Add.Visible = true;
            this.Save.Visible = false;
            String sql1="";
            if (this.LuHao.Text == "")
            {
                sql1 = @"SELECT [ID],[炉号], [C], [Mn], [Si], [P], [S], [Cr], [Ni], [Cu], [Mo], [Al], [V], [Bi], [Sn], [Pb], [As], [Sb], [抗拉强度], [屈服极限], [伸长率] FROM [产品理化参数] where 产品ID='" + this.ChanPin.SelectedValue + "'";
               // this.Message.Text = ".请输入炉号！";
               // return;
                
            }
            else
            {
                sql1 = @"SELECT [ID],[炉号], [C], [Mn], [Si], [P], [S], [Cr], [Ni], [Cu], [Mo], [Al], [V], [Bi], [Sn], [Pb], [As], [Sb], [抗拉强度], [屈服极限], [伸长率] FROM [产品理化参数] where 产品ID='" + this.ChanPin.SelectedValue + "' and 炉号='" + this.LuHao.Text + "'";
            } this.SqlDataSource1.SelectCommand = sql1;
            this.ViewState["SelectCommand1"] = sql1;          
            this.SqlDataSource1.SelectCommand = (string)this.ViewState["SelectCommand1"];
            Panel0.Visible = true;
        }

        //返回按钮
        protected void Back_Click(object sender, EventArgs e)
        {
            this.Add.Visible = true;
            this.Edit.Visible = true;
            this.Save.Visible = false;
            this.Delete.Visible = true;
            this.Find.Visible = true;
            this.Panel0.Visible = true;
            this.Panel2.Visible = false;
            this.Back.Visible = false;
        }

        //修改
        protected void Edit_Click(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
            { 
               // this.LuHao0.Enabled = false;
                //Response.Redirect("产品理化参数.aspx?ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString()+"&type="+Request.QueryString["type"]);
                this.Panel2.GroupingText = "修 改";
                this.Panel2.Visible = true;
                string ID = "";               
                try
                {
                    ID = GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString();
                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    SqlDataAdapter sda = new SqlDataAdapter("Select * From 产品理化参数 where ID=" + ID, Conn);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    LuHao0.Text = ds.Tables[0].Rows[0][2].ToString();
                    C.Text = ds.Tables[0].Rows[0][3].ToString();
                    Mn.Text = ds.Tables[0].Rows[0][4].ToString();
                    Si.Text = ds.Tables[0].Rows[0][5].ToString();
                    P.Text = ds.Tables[0].Rows[0][6].ToString();
                    S.Text = ds.Tables[0].Rows[0][7].ToString();
                    Cr.Text = ds.Tables[0].Rows[0][8].ToString();
                    Ni.Text = ds.Tables[0].Rows[0][9].ToString();
                    Cu.Text = ds.Tables[0].Rows[0][10].ToString();
                    Mo.Text = ds.Tables[0].Rows[0][11].ToString();
                    Al.Text = ds.Tables[0].Rows[0][12].ToString();
                    V.Text = ds.Tables[0].Rows[0][13].ToString();
                    Bi.Text = ds.Tables[0].Rows[0][14].ToString();
                    Sn.Text = ds.Tables[0].Rows[0][15].ToString();
                    Pb.Text = ds.Tables[0].Rows[0][16].ToString();
                    As.Text = ds.Tables[0].Rows[0][17].ToString();
                    Sb.Text = ds.Tables[0].Rows[0][18].ToString();
                    klqd.Text = ds.Tables[0].Rows[0][19].ToString();
                    qsjx.Text = ds.Tables[0].Rows[0][20].ToString();
                    scl.Text = ds.Tables[0].Rows[0][21].ToString();
                    Panel0.Visible = false;
                    this.Edit.Visible = false;
                    this.Find.Visible = false;
                    this.Save.Visible = true;
                    this.Add.Visible = false;
                    this.Delete.Visible = false;
                    this.Back.Visible = true;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    this.Panel2.Visible = false;                
                    this.Message.Text = "·请先选择一个项目";
                }
                catch (Exception ex)
                {
                    this.Message.Text = ".该炉号信息不存在！";
                }
            }
            else
            {
                this.Message.Text = "·请先选择一个项目";
            }
        }



      
    }
}
